#coding=utf-8
import tensorflow as tf
import numpy as np
import pprint

sess = tf.InteractiveSession()
#tf.ones 生成 shape 数据结构 dtype 数据类型
# x = tf.ones(shape=[2,3],dtype=np.int32) #生成两行三列的以1填充的数组
# y = tf.zeros(shape=[4,2],dtype=np.int32) #生成四行两列的以1填充的数组
# pprint.pprint(sess.run(x))
# pprint.pprint(sess.run(y))

# tf.ones_like 生成一个以1填充的结构与tensor一模一样的 数组
# tf.zeros_like 生成一个以1填充的结构与tensor一模一样的 数组
# tensor = [[1,2,3,2],[4,5,6,4]]
# x = tf.ones_like(tensor)
# pprint.pprint(sess.run(x))
# y = tf.zeros_like(tensor)
# pprint.pprint(sess.run(y))

# tf.fill 创建一个结构为dims 数值为value的tensor
# pprint.pprint(sess.run(tf.fill(dims=[2,3],value=10)))

# 创建一个常量 按照value复制,结构为shape,如果value是一个数字则所有的值登录value,如果value是一个list则按照顺序赋值,如果长度不够,则登录最后一个值
# a = tf.constant(2,shape=[3])
# b = tf.constant(2,shape=[1,4])
# c = tf.constant([1,2,3],shape=[1,6])
# d = tf.constant([1,2,3],shape=[3,2])

# pprint.pprint(sess.run(a))
# pprint.pprint(sess.run(b))
# pprint.pprint(sess.run(c))
# pprint.pprint(sess.run(d))

#random_normal 正太分布随机数 均值mean,标准差stddev
# x = tf.random_normal(shape=[1,5],mean=0.0,stddev=1.0,dtype=tf.float32,seed=None,name=None)
# pprint.pprint(sess.run(x))

#truncated_normal 截断正太分布随机数,均值mean,标准差stddev
# y = tf.truncated_normal(shape=[1,5],mean=0.0,stddev=1.0,dtype=tf.float32,seed=None,name=None)
# pprint.pprint(sess.run(y))

# random_uniform 均匀分布随机数,范围minval-maxval
# y = tf.random_uniform([1,6],minval=-10,maxval=10,dtype=tf.int32,seed=None,name=None)
# pprint.pprint(sess.run(y))

# tf.shape 获取张量的形状
# labels = [[1,2],[3,4]]
# shape = tf.shape(labels)
# pprint.pprint(sess.run(shape))

# 张量维度+1 一次只能加一维
# labels = [1,2,3]
# x = tf.expand_dims(labels,0)
# pprint.pprint(sess.run(x))
# x = tf.expand_dims(labels,1)
# pprint.pprint(sess.run(x))
# x = tf.expand_dims(sess.run(x),2)
# pprint.pprint(sess.run(x))

#tf.concat 将数组按照指定下标组成新的数组
# t1 = [[1,2,3],[4,5,6]]
# t2 = [[7,8,9],[10,11,12]]
# result = tf.concat([t1,t2],0)
# pprint.pprint(result)

# tf.sparse_to_dense 稀疏矩阵转密集矩阵

# 沿着value的第一维进行随机重新排列
# a = [[1,2],[3,4],[5,6]]
# x = tf.random_shuffle(a)
# pprint.pprint(sess.run(x))

# 找到指定的张量在指定轴axis上的最大值/最小值
# a = tf.get_variable(name='a',
#                     shape=[3,4],
#                     dtype=tf.float32,
#                     initializer=tf.random_uniform_initializer(minval=-1,maxval=1))
# b=tf.argmax(input=a,dimension=0)
# c=tf.argmax(input=a,dimension=1)
# sess.run(tf.initialize_all_variables())
# pprint.pprint(sess.run(a))
# pprint.pprint(sess.run(b))
# pprint.pprint(sess.run(c))

# tf.equal 判断两个tensor是否每个元素都相等
# pprint.pprint(tf.equal(tf.ones([2,3],tf.int32),tf.ones([2,3],tf.int32)))

# tf.cast 将a的数值用dtype来格式化
# a = tf.Variable([1,0,0,1,1])
# b = tf.cast(a,dtype=tf.bool)
# sess.run(tf.initialize_all_variables())
# pprint.pprint(sess.run(b))

# 矩阵乘法
# a = tf.random_uniform([1,2],minval=-1,maxval=1,dtype=tf.int32)
# b = tf.random_uniform([2,3],minval=1,maxval=10,dtype=tf.int32)
# c = tf.matmul(a,b)
# pprint.pprint(sess.run(a))
# pprint.pprint(sess.run(b))
# pprint.pprint(sess.run(c))


# 将tensor按照新的shape重新排列 shape=[-1],将tensor展开成一个list
# shape=[a,b,c,....] 按照正常方法排列
# shape=[a,-1] -1由计算得到
# t = [1,2,3,4,5,6,7,8,9]
# a = tf.reshape(t,[3,3])

# t = [[[1,1],[2,2]],[[3,3],[4,4]]]
# b = tf.reshape(t,[2,4])

# t = [[[1, 1, 1],
#                 [2, 2, 2]],
#                [[3, 3, 3],
#                 [4, 4, 4]],
#                [[5, 5, 5],
#                 [6, 6, 6]]]
# c = tf.reshape(t,[-1])

# d = tf.reshape(t,[2,-1])

# e = tf.reshape(t,[-1,9])

# f = tf.reshape(t,[2,-1,3])

# pprint.pprint(sess.run(a))
# pprint.pprint(sess.run(b))
# pprint.pprint(sess.run(c))
# pprint.pprint(sess.run(d))
# pprint.pprint(sess.run(e))
# pprint.pprint(sess.run(f))

# tf.trainable_variables() 获取所有可训练变量
# a = tf.get_variable('a',shape=[5,2])
# b = tf.get_variable('b',shape=[2,5],trainable=False)
# c = tf.constant([1,2,3],dtype=tf.int32,shape=[8],name='c')
# d = tf.Variable(tf.random_uniform(shape=[3,3]),name='d')
# tvar = tf.trainable_variables()
# tvar_name = [x.name for x in tvar]
# print(tvar)
# print(tvar_name)

# sess.run(tf.initialize_all_variables())
# pprint.pprint(sess.run(tvar))

# tf.nn.dropout() 按概率将x中一些元素值置零,并将其他值放大 一定程度上防止过拟合
# a = tf.get_variable('a',shape=[2,5])
# b = a
# a_drop = tf.nn.dropout(a,0.8)
# sess.run(tf.initialize_all_variables())
# print(sess.run(b))
# print(sess.run(a_drop))

# tf.linspace(start,stop,num,name) 在[start,stop]范围内产生num个数的等差数列
# tf.range(start,limit,delta) 在[start,limit]以delta为步进值 产生等差数列
# x = tf.linspace(start=1.0,stop=5.0,num=6,name=None)
# y = tf.range(start=1,limit=5,delta=1)
# print(sess.run(x))
# print(sess.run(y))

# tf.assign 用来更新模型中变量的值 ref=value
# a = tf.Variable(0.0)
# b = tf.placeholder(dtype=tf.float32,shape=[])
# op = tf.assign(a,b)

# sess.run(tf.initialize_all_variables())
# print(sess.run(a))

# sess.run(op,feed_dict={b:5.})
# print(sess.run(a))